package cn.da.shuai.cool.finance.service.impl;

import cn.da.shuai.cool.common.expection.CoolServiceException;
import cn.da.shuai.cool.finance.mapper.CoolBudgetControlMapper;
import cn.da.shuai.cool.finance.model.CoolBudgetControl;
import cn.da.shuai.cool.finance.service.ICoolBudgetControlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CoolBudgetControlServiceImpl extends ServiceImpl<CoolBudgetControlMapper, CoolBudgetControl> implements ICoolBudgetControlService {

    @Override
    public List<CoolBudgetControl> getActiveBudgetControls(Long departmentId, String category, Integer fiscalYear, Integer fiscalMonth) {
        LambdaQueryWrapper<CoolBudgetControl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CoolBudgetControl::getDepartmentId, departmentId)
                .eq(CoolBudgetControl::getCategory, category)
                .eq(CoolBudgetControl::getFiscalYear, fiscalYear)
                .eq(CoolBudgetControl::getFiscalMonth, fiscalMonth)
                .eq(CoolBudgetControl::getActive, true);

        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyBudgetControl(CoolBudgetControl budgetControl) {
        // 验证预算控制参数
        validateBudgetControl(budgetControl);

        // 如果是新建，直接保存
        if (budgetControl.getId() == null) {
            this.save(budgetControl);
            return;
        }

        // 如果是更新，检查是否存在
        CoolBudgetControl existingControl = this.getById(budgetControl.getId());
        if (existingControl == null) {
            throw new CoolServiceException("预算控制记录不存在");
        }

        // 更新预算控制
        this.updateById(budgetControl);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deactivateBudgetControl(Long budgetControlId) {
        CoolBudgetControl control = this.getById(budgetControlId);
        if (control == null) {
            throw new CoolServiceException("预算控制记录不存在");
        }

        control.setActive(false);
        this.updateById(control);
    }

    private void validateBudgetControl(CoolBudgetControl budgetControl) {
        if (budgetControl.getDepartmentId() == null) {
            throw new CoolServiceException("部门ID不能为空");
        }

        if (budgetControl.getCategory() == null || budgetControl.getCategory().trim().isEmpty()) {
            throw new CoolServiceException("预算类别不能为空");
        }

        if (budgetControl.getFiscalYear() == null) {
            throw new CoolServiceException("财政年度不能为空");
        }

        if (budgetControl.getFiscalMonth() == null) {
            throw new CoolServiceException("财政月份不能为空");
        }

        if (budgetControl.getControlType() == null || budgetControl.getControlType().trim().isEmpty()) {
            throw new CoolServiceException("控制类型不能为空");
        }

        // 检查控制类型是否有效
        String controlType = budgetControl.getControlType();
        if (!controlType.equals("HARD_LIMIT") &&
            !controlType.equals("SOFT_LIMIT") &&
            !controlType.equals("NOTIFICATION_ONLY") &&
            !controlType.equals("NO_CONTROL")) {
            throw new CoolServiceException("无效的控制类型");
        }

        // 如果需要审批，确保有审批流程ID
        if (Boolean.TRUE.equals(budgetControl.getApprovalRequired()) && budgetControl.getApprovalWorkflowId() == null) {
            throw new CoolServiceException("需要审批时，审批流程ID不能为空");
        }
    }
}